package org.oslims.client.uicomponents.editbox;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.oslims.client.uicomponents.inputfields.DateInputField;
import org.oslims.client.uicomponents.inputfields.DropDownInputField;
import org.oslims.client.uicomponents.inputfields.InputField;
import org.oslims.client.uicomponents.inputfields.RichTextInputField;
import org.oslims.client.uicomponents.inputfields.TextInputField;
import org.oslims.domain.Request;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditRequestBox {

	final DialogBox editRequestBox = new DialogBox(false, true);
	final VerticalPanel editRequestBoxPanel = new VerticalPanel();

	protected Map<String, InputField> inputFields = new HashMap<String, InputField>();

	final HorizontalPanel buttonLine = new HorizontalPanel();
	final Button saveButton = new Button("Save");
	final Button cancelButton = new Button("Cancel");

	final TextInputField subjectField = new TextInputField("subject", "Subject: ", "80px", "200px");
	final RichTextInputField descriptionField = new RichTextInputField("description", "Description: ", "80px", "400px");
	final DateInputField deadlineField = new DateInputField("deadline", "Deadline: ", "80px", "200px");
	final DropDownInputField requestorField = new DropDownInputField("requestor", "Requestor: ", "80px", "200px");
	final DropDownInputField stateField = new DropDownInputField("state", "State: ", "80px", "200px");
	final DropDownInputField priorityField = new DropDownInputField("priority", "Priority: ", "80px", "200px");

	Request editedRequest;

	String editedType;

	public EditRequestBox() {

		editRequestBoxPanel.add(subjectField);
		inputFields.put(subjectField.getFieldName(), subjectField);

		editRequestBoxPanel.add(descriptionField);
		inputFields.put(descriptionField.getFieldName(), descriptionField);

		editRequestBoxPanel.add(deadlineField);
		inputFields.put(deadlineField.getFieldName(), deadlineField);

		editRequestBoxPanel.add(requestorField);
		inputFields.put(requestorField.getFieldName(), requestorField);

		List<String> states = new ArrayList<String>();
		states.add("Not Released");
		states.add("Information Missing");

		stateField.setSelectionItems(states);
		editRequestBoxPanel.add(stateField);
		inputFields.put(stateField.getFieldName(), stateField);

		List<String> priorities = new ArrayList<String>();
		priorities.add("Standard");
		priorities.add("High");

		priorityField.setSelectionItems(priorities);
		editRequestBoxPanel.add(priorityField);
		inputFields.put(priorityField.getFieldName(), priorityField);

		buttonLine.add(saveButton);
		buttonLine.add(cancelButton);

		editRequestBox.add(editRequestBoxPanel);

		// Add the save and cancel buttons
		editRequestBoxPanel.add(buttonLine);
	}

	public void hide() {
		editRequestBox.hide();
	}

	public void clear() {
		editedRequest = null;
		subjectField.getInputField().setValue("");
		descriptionField.getInputHtml().setHTML("");
		stateField.getInputBox().setSelectedIndex(-1);
		priorityField.getInputBox().setSelectedIndex(-1);
	}

	public void showNew() {
		this.clear();
		this.clearAllErrors();
		editRequestBox.setText("Create new Request");
		editRequestBox.center();
	}

	public void showEdit(Request request) {
		this.clear();
		this.clearAllErrors();

		editedRequest = request;
		subjectField.getInputField().setValue(request.getSubject());
		descriptionField.getInputHtml().setHTML(request.getDescription());
		if (request.getDeadline() != null)
			deadlineField.getInputField().setValue(request.getDeadline());

		for (int i = 0; i < stateField.getInputBox().getItemCount(); i++) {
			if (stateField.getInputBox().getItemText(i).equals(request.getState())) {
				stateField.getInputBox().setSelectedIndex(i);
				break;
			}
		}

		for (int i = 0; i < priorityField.getInputBox().getItemCount(); i++) {
			if (priorityField.getInputBox().getItemText(i).equals(request.getPriority())) {
				priorityField.getInputBox().setSelectedIndex(i);
				break;
			}
		}

		editRequestBox.setText("Edit Request");
		editRequestBox.center();
	}

	public void showError(String fieldName, String message) {
		GWT.log(fieldName);
		inputFields.get(fieldName).showError(message);
	}

	public void clearError(String fieldName) {
		inputFields.get(fieldName).clearError();
	}

	public void clearAllErrors() {
		for (InputField inputField : inputFields.values()) {
			inputField.clearError();
		}
	}

	public HasValue<String> getSubject() {
		return subjectField.getInputField();
	}

	public HasHTML getDescription() {
		return descriptionField.getInputHtml();
	}

	public HasValue<Date> getDeadline() {
		return deadlineField.getInputField();
	}

	public DropDownInputField getState() {
		return stateField;
	}

	public DropDownInputField getPriority() {
		return priorityField;
	}

	public DropDownInputField getRequestor() {
		return requestorField;
	}

	public HasClickHandlers getSaveButton() {
		return saveButton;
	}

	public HasClickHandlers getCancelButton() {
		return cancelButton;
	}

	public Request getRequest() {
		return editedRequest;
	}
}
